﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyMovieInfo
{
    class MovieResolver
    {
        private List<Movie> _movies = new List<Movie>();
        private List<string> _invalidfiles = new List<string>();

        const string movieregex = @"^(?<path>.*\\)(?<name>.+?)\((?<year>\d{4})\)\.(?<ext>\w{3})$";

        public MovieResolver(List<string> fileList)
        {
            //parse the names of the movies
            CreateMovieListFromFileNames(fileList);
        }

        public List<Movie> Movies
        {
            get { return this._movies; }
        }

        private void CreateMovieListFromFileNames(List<string> fileList)
        {
            foreach (var moviefile in fileList)
            {
                //TODO: Use compiled regex
                MatchCollection matches = Regex.Matches(moviefile, movieregex, RegexOptions.IgnoreCase);

                if (matches != null && matches.Count > 0)
                {
                    string moviename = matches[0].Groups["name"].Value.Trim();
                    //TODO: Add Error handling or TryParse
                    int movieyear = Int32.Parse(matches[0].Groups["year"].Value);
                    var movie = new Movie()
                      {
                          FilePath = moviefile,
                          Name = moviename,
                          Year = movieyear,
                          MatchFound = false
                      };

                    _movies.Add(movie);
                }
                else
                {
                    _invalidfiles.Add(moviefile);
                }
            }
        }

        internal void QueryNetFlix()
        {
            //OCP Violation - Can't add another movie service without changing the Movie Information Class

            //hit the odata feed for netflix.
            var catalog = new NetflixService.NetflixCatalog(
                new Uri("http://odata.netflix.com/Catalog/",
                    UriKind.Absolute));

            for (int i = 0; i < _movies.Count; i++)
            {
                var response = (from p in catalog.Titles
                                where p.Name == _movies[i].Name
                                    && p.ReleaseYear == _movies[i].Year
                                    && p.ShortSynopsis != null
                                select p).Take(1);

                if (response.Count() == 1)
                {
                    var title = response.Single<NetflixService.Title>();

                    _movies[i].MatchFound = true;
                    _movies[i].Name = title.Name;
                    _movies[i].CoverURL = title.BoxArt.LargeUrl;
                    _movies[i].Synopsis = title.ShortSynopsis;
                }
                else
                {
                    _movies[i].MatchFound = false;
                }
            }
        }
        
        internal void PersistMovieData()
        {
            throw new NotImplementedException();
        }

    }
}